<?php
/**
 * Created by PhpStorm.
 * User: maqun
 * Date: 2016/1/9
 * Time: 17:43
 * 用户登录，权限管理
 */

class UserAction extends BaseAction{

    public function _initialize(){
        self::$action = 4;
        parent::_initialize();
    }
    /**
     * 用户登录
     */
    public function login(){

        $post = $this->_post();
        if(empty($post['user_name'])&&empty($post['password'])){
            if(!empty($_SESSION['USER'])){
                redirect(U('/Index/index/'));
            }
            $this->display('Public/login');
            exit;
        }
        $user_name = $post['user_name'];
        $password  = $post['password'];

        $return = array('status'=>'','info'=>'');
        if(!empty($user_name) && !empty($password)){
            $info = D('AdminUsers')->checkLogin($user_name,$password);

            if(false!==$info){
                $_SESSION['USER'] = $info;
                $return['status'] = true;
                $return['info']   = "登录成功";
                $return['url']    = U('Index/index');
            }else{
                $return['status'] = false;
                $return['info'] = "用户名或密码错误";
            }

        }else{
            $return['status'] = false;
            $return['info'] = "用户名或密码错误";
        }
        $this->ajaxReturn($return);
    }
    /**
     * 退出登录
     */
    public function loginOut(){
        session_destroy();
        redirect('User/login');
    }

    /**
     * 获取管理员列表
     */
    public function getAdmin(){

        $name = $this->_get('condition');
        $where = !empty($name) ? " locate(user_name,'{$name}') and del=1": ' del=1';

        $list = D('AdminUsers')->getAdminList($where);

        $this->assign('list',$list);
        $this->display('index');

    }

    /**
     * 添加管理员,显示添加页面
     */
    public function add(){

        $this->display('add');

    }
    /**
     * 修改管理员信息，显示修改页面
     */
    public function edit(){

        $id = $this->_get('id');

        $id = intval($id);
        $info  = D('AdminUsers')->getInfoById($id);
        if(empty($info)){
            $this->error('数据不存在');
        }
        $this->assign('info',$info);
        $this->display('edit');
    }

    /**
     * 删除管理员
     */
    public function delete(){

        $id = $this->_get('id');
        $id = intval($id);
        $info  = D('AdminUsers')->getInfoById($id);
        if(empty($info)){
            $this->error('数据不存在');
        }
        $res = D('AdminUsers')->where('id='.$id)->delete();
        if(false !==$res){
            $this->success('删除成功',U('/User/getAdmin/'));
        }else{
            $this->error('删除失败');
        }
    }
    /**
     * 保存添加修改数据
     */
    public function save(){
        $data = $this->_post();

        $res  = D('AdminUsers')->saveAdmins($data);

        if($res['status']){
            $this->success($res['info'],U('/User/getAdmin/'));
        }else{
            $this->error($res['info']);
        }
    }

    /**
     * 修改密码
     */
    public function pass(){
        $data = $this->_post();

        if(empty($data)){
            $this->display('pass');
            exit;
        }

        $res  = D('Users')->changePass($data);

        if($res['status']){
            $this->success($res['info']);
        }else{
            $this->error($res['info']);
        }
    }
}